package red.stu.pconstraint.dto.mapper;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import red.stu.pconstraint.dto.mapper.block.BaseNodeBlock;
import red.stu.pconstraint.dto.mapper.block.WhenBlock;

import java.util.List;

/**
 * 流程模版分支节点
 * @author yihrmc @stu.red
 * @date 2019/11/15 9:20
 */
@Getter
@Setter
@ToString
@Accessors(chain = true)
public class BranchNode {

    /**
     * 节点ID
     */
    @NotNull
    private String id;

    /**
     * 节点描述
     */
    @Nullable
    private String text;

    /**
     * 是否是位于第一个
     */
    @Nullable
    private Boolean first;

    /**
     * 是否是位于最后一个
     */
    @Nullable
    private Boolean last;

    /**
     * 节点拥有的所有块
     */
    @NotNull
    private List<BaseNodeBlock> blocks;

    /**
     * 同级节点 下一个节点 <br/>
     *  - 如果是最后一个节点则null
     */
    @Nullable
    private BranchNode nextBranchNode;

    /**
     * 同级节点 上一个节点 <br/>
     *     - 如果是第一个节点则null
     */
    @Nullable
    private BranchNode previousBranchNode;

    public boolean isFirst() {
        return first;
    }

    public boolean isLast() {
        return last;
    }

    public WhenBlock getWhenBlock(@NotNull String stateName) {
        for (BaseNodeBlock block : blocks) {
            if (block instanceof WhenBlock) {
                WhenBlock whenBlock = (WhenBlock) block;
                if (stateName.equals(whenBlock.getStateName())) {
                    return whenBlock;
                }
            }
        }
        return null;
    }

}
